package com.example.goodog.adapters;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.graphics.ColorFilter;
import android.graphics.LightingColorFilter;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.goodog.MyApplication;
import com.example.goodog.R;
import com.example.goodog.Utils;
import com.example.goodog.activities.UserActivity;
import com.example.goodog.activities.WizardActivity;
import com.example.goodog.customViews.ChangeLocationButton;
import com.example.goodog.customViews.EditButton;
import com.example.goodog.interfaces.IShowChangeLocationButton;
import com.example.goodog.interfaces.IShowEditButton;
import com.example.goodog.items.DogItem;
import com.example.goodog.items.ListItem;



public class UserDogAdapter extends ArrayAdapter<ListItem> {
	protected Context context;
	protected ArrayList<DogItem> items = new ArrayList<DogItem>();
	private int LayoutID;
	public UserDogAdapter(Context c, int layoutID) {
		super (c, layoutID);
		LayoutID = layoutID;
		this.items = MyApplication.getUser().Dogs;
		context = c;

	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public int getCount() {
		return items.size();
	}
	@Override
	public ListItem getItem(int arg0) {
		return items.get(arg0);
	}
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		View List;
		if(convertView==null){
			List=new View(context);
			LayoutInflater mLayoutinflater=(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			List=mLayoutinflater.inflate(LayoutID, parent, false);
		}
		else{
			List = (View)convertView;
		}

		final DogItem item = ((DogItem)items.get(position));

		TextView DogNameText = (TextView)List.findViewById(R.id.dogNameTextView);
		DogNameText.setText(item.Name);


		ColorFilter Colorfilter = new LightingColorFilter( Color.BLUE, Color.BLUE );

		((ImageView)List.findViewById(R.id.imageView_xsmall)).setImageResource(R.drawable.xsmall);
		((ImageView)List.findViewById(R.id.imageView_small)).setImageResource(R.drawable.small);
		((ImageView)List.findViewById(R.id.imageView_medium)).setImageResource(R.drawable.medium);
		((ImageView)List.findViewById(R.id.imageView_large)).setImageResource(R.drawable.large);
		((ImageView)List.findViewById(R.id.imageView_xlarge)).setImageResource(R.drawable.xlarge);
		((ImageView)List.findViewById(R.id.imageView_xxlarge)).setImageResource(R.drawable.xxlarge);

		ImageView SelectedImage = (ImageView)List.findViewById(R.id.imageView_xsmall);
		Drawable myIcon =  context.getResources().getDrawable( R.drawable.xsmall_color );

		switch (item.Size) {
		case 1:
			SelectedImage = (ImageView)List.findViewById(R.id.imageView_xsmall);
			myIcon =  context.getResources().getDrawable( R.drawable.xsmall_color );
			break;
		case 2:
			SelectedImage = (ImageView)List.findViewById(R.id.imageView_small);
			myIcon =  context.getResources().getDrawable( R.drawable.small_color );
			break;
		case 3:
			SelectedImage = (ImageView)List.findViewById(R.id.imageView_medium);
			myIcon =  context.getResources().getDrawable( R.drawable.medium_color );
			break;
		case 4:
			SelectedImage = (ImageView)List.findViewById(R.id.imageView_large);
			myIcon =  context.getResources().getDrawable( R.drawable.large_color );
			break;
		case 5:
			SelectedImage = (ImageView)List.findViewById(R.id.imageView_xlarge);
			myIcon =  context.getResources().getDrawable( R.drawable.xlarge_color );
			break;
		case 6:
			SelectedImage = (ImageView)List.findViewById(R.id.imageView_xxlarge);
			myIcon =  context.getResources().getDrawable( R.drawable.xxlarge_color );
			break;
		}

		myIcon.setColorFilter(Colorfilter);
		SelectedImage.setImageDrawable(myIcon);



		if(item.Sex)
		{
			((ImageView)List.findViewById(R.id.imageView_sex)).setImageResource(R.drawable.male);
		}
		else
		{
			((ImageView)List.findViewById(R.id.imageView_sex)).setImageResource(R.drawable.female);
		}


		TextView DogAgeText = (TextView)List.findViewById(R.id.textViewDogAge);
		Date DogBirthDate = Utils.StringToDate(item.DateOfBirth, "MM/yy");
        Date now = new Date();
        int monthDiff = Utils.monthsBetween(now,DogBirthDate);
        DogAgeText.setText(String.format("Age: %d,%d",monthDiff/12,monthDiff%12));
		

		ImageView DogImage = (ImageView) List.findViewById(R.id.dogImage);
		Bitmap bmp = item.getBitmap();
		if(bmp != null)
		{
			DogImage.setImageBitmap(bmp);
		}

		AddButtonsToToolbar(List, item);


		return List;
	}


	public String GetTitle()
	{
		return "Missing Tilte";
	}

	public void AddButtonsToToolbar(View List, ListItem item)
	{
		// Resets the toolbar to be closed
		LinearLayout toolbar = (LinearLayout) List.findViewById(R.id.toolbar);
		toolbar.removeAllViews();
		
		
		if(item instanceof IShowEditButton)
		{
			toolbar.addView(new EditButton(context, ((IShowEditButton)item).GetInstance(), WizardActivity.EDIT_DOG));
		}
		
		if(item instanceof IShowChangeLocationButton)
		{
			toolbar.addView(new ChangeLocationButton(context, item.ID, UserActivity.CHANGE_DOG_LOCATION));
		}
		
		

		((LinearLayout.LayoutParams) toolbar.getLayoutParams()).bottomMargin = -50;
		toolbar.setVisibility(View.GONE);
	}

	private void SetList()
	{
		if(MyApplication.getUser() != null)
		{
			this.items = MyApplication.getUser().Dogs;
		}
	}
}
